﻿// Copyright (C) 2010-2012, MaGUI
//
//  Elena Ballesteros Molinero
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.IO;
using System.Windows.Forms;

namespace Magui
{
    class Configuration
    {
        public bool Portable { get; set; }
        /// <summary>
        /// Name of the test file used to load/save the customer object.
        /// </summary>
        private readonly string _xmlPublicConfigPath;
        private readonly string _xmlPrivateConfigPath;

        private static Configuration _instance;
        public static Configuration Instance
        {
            get { return _instance ?? (_instance = new Configuration()); }
        }

        public Configuration()
        {
            var directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                            "magui");
            string fileName = "publicSettings.xml";
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);                               
            }

            _xmlPublicConfigPath = Path.Combine(directory, fileName);
            if (!File.Exists(_xmlPublicConfigPath)) File.Copy(Path.Combine(Application.StartupPath, fileName), _xmlPublicConfigPath);

            _xmlPrivateConfigPath = Path.Combine(directory, "privateSettings.xml");
        }

        public void LoadPubliConfig()
        {
            // Load the customer object from the existing XML file (if any)...
            if (File.Exists(_xmlPublicConfigPath))
            {
                PublicSettings settings = ObjectXMLSerializer<PublicSettings>.Load(_xmlPublicConfigPath);
                settings.SetPublicSettings();              
            }
            else
            {
                // El fichero se carga automáticamente al lanzar la aplicación
                //MessageBox.Show(@"No existe ningún fichero de configuración");
            }
        }

        public void SavePublicConfig()
        {
            // Create customer object based on Form values.
            var settings = new PublicSettings();
            settings.GetPublicSettings();

            //Save customer object to XML file using our ObjectXMLSerializer class...
            try
            {
                ObjectXMLSerializer<PublicSettings>.Save(settings, _xmlPublicConfigPath);
             //   MessageBox.Show(@"El fichero de configuración se ha guardado con éxito");
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"No ha sido posible guardar el fichero de configuración");
            }
        }

        /*private void LoadPrivateConfig()
        {
            // Load the customer object from the existing XML file (if any)...
            if (File.Exists(_xmlPrivateConfigPath))
            {
                PrivateSettings settings = ObjectXMLSerializer<PrivateSettings>.Load(_xmlPrivateConfigPath);
                settings.SetPrivateSettings();
            }
            else
            {
                MessageBox.Show(@"No existe ningún fichero de configuración");
            }
        }

        private void SavePrivateConfig()
        {
            // Create customer object based on Form values.
            var settings = new PrivateSettings();
            settings.GetPrivateSettings();

            //Save customer object to XML file using our ObjectXMLSerializer class...
            try
            {
                ObjectXMLSerializer<PrivateSettings>.Save(settings, _xmlPrivateConfigPath);
                //MessageBox.Show(@"El fichero de configuración se ha guardado con éxito");
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"No ha sido posible guardar el fichero de configuración");
            }
        }
        */
        /*
        public void LoadConfig()
        {
            LoadPubliConfig();
            LoadPrivateConfig();
        }

        public void SaveConfig()
        {
            SavePublicConfig();
            SavePrivateConfig();
        }*/
    }
}